from News.models import *
from rest_framework import serializers
from datetime import datetime
##继承序列化类
from czllblog import settings
from UserProfile.models import User

#类型序列
class TypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Type
        #序列化所有字段
        fields = ('id', 'name')

#列表序列
class NewsSerializer(serializers.ModelSerializer):
    cover = serializers.SerializerMethodField()#封面
    ctime = serializers.SerializerMethodField()#创建时间
    #类型
    type = serializers.IntegerField()
    type_name = serializers.SerializerMethodField()
    #用户
    userinfo = serializers.SerializerMethodField()
    class Meta:
        model = News
        #序列化所有字段
        fields = ('id','type','type_name','userinfo','cover','title','is_recommend','ctime','view','comments','source','goods','content')
    #图片链接拼接
    def get_cover(self, obj):
        rel_path = obj['cover']
        rely_on_path = settings.MEDIA_URL
        return rely_on_path + rel_path
    #处理时间
    def get_ctime(self,obj):
        old_ctime = obj['ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime
    #获取外键类型信息
    def get_type_name(self,obj):
        type = obj['type']
        type_res = Type.objects.filter(id=type).values('name')[:1][0]
        # print()
        return type_res['name']
    def get_userinfo(self,obj):
        user = obj['user']
        user_res = User.objects.filter(id=user).values('id','username','nickname','avatar')[:1][0]
        rely_on_path = settings.MEDIA_URL
        user_res['avatar'] = rely_on_path +  user_res['avatar']
        return user_res

#详情序列
class NewsDtailSerializer(serializers.ModelSerializer):
    cover = serializers.SerializerMethodField()#封面
    ctime = serializers.SerializerMethodField()#创建时间
    #类型
    type = serializers.IntegerField()
    type_name = serializers.SerializerMethodField()
    #用户
    userinfo = serializers.SerializerMethodField()
    #评论
    comment = serializers.SerializerMethodField()
    class Meta:
        model = News
        #序列化所有字段
        fields = ('id','type','type_name','userinfo','cover','title','is_recommend','ctime','view','comments','source','goods','content','comment')
    #图片链接拼接
    def get_cover(self, obj):
        rel_path = obj['cover']
        rely_on_path = settings.MEDIA_URL
        return rely_on_path + rel_path
    #处理时间
    def get_ctime(self,obj):
        old_ctime = obj['ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime
    #获取外键类型信息
    def get_type_name(self,obj):
        type = obj['type']
        type_res = Type.objects.filter(id=type).values('name')[:1][0]
        # print()
        return type_res['name']
    def get_userinfo(self,obj):
        user = obj['user']
        user_res = User.objects.filter(id=user).values('id','username','nickname','avatar')[:1][0]
        rely_on_path = settings.MEDIA_URL
        user_res['avatar'] = rely_on_path + user_res['avatar']
        return user_res

    #获取评论信息
    def get_comment(self,obj):
        news_id = obj['id']
        res = NComment.objects.filter(news=news_id, topic=0).values('id', 'topic', 'news', 'post', 'user',
                                                                          'content', 'ctime').order_by('ctime')
        if res:
            ser = CommtentSerializer(res, many=True)
            data = ser.data
        else:
            data = None
        return data

#评论序列
class CommtentSerializer(serializers.ModelSerializer):
    userinfo = serializers.SerializerMethodField()
    children = serializers.SerializerMethodField()
    ctime = serializers.SerializerMethodField()
    post = serializers.IntegerField()
    class Meta:
        model = NComment
        #序列化所有字段
        fields = ('id','topic','post','userinfo','children','content','ctime')
    def get_ctime(self,obj):
        old_ctime = obj['ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime
    def get_userinfo(self,obj):
        user = obj['user']
        user_res = User.objects.filter(id=user).values('id','username','nickname','avatar')[:1][0]
        user_res['avatar'] = settings.MEDIA_URL+user_res['avatar']
        return user_res
    def get_children(self,obj):
        id = obj['id']
        topic = obj['topic']
        print(topic)
        new_topic = topic + 1
        news = obj['news']
        comment_children = NComment.objects.filter(news=news,topic=new_topic,post=id).values('id','topic', 'news','post','user','content','ctime').order_by('ctime')
        if comment_children:
            ser = CommtentSerializer(comment_children,many=True)
            data = ser.data
        else:
            data = None
        return data


class NewsHotSerializer(serializers.ModelSerializer):
    ctime = serializers.SerializerMethodField()  # 创建时间
    class Meta:
        model = News
        #序列化所有字段
        fields = ('id','title','ctime')

    #处理时间
    def get_ctime(self,obj):
        old_ctime = obj['ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime